<?php
namespace app\queue\redis;

use Webman\RedisQueue\Consumer; 
use support\Log;
use Webman\RedisQueue\Redis;
use Webman\RedisQueue\Client; 
use support\Db;  

class ZjConsumer implements Consumer
{
    // 要消费的队列名 
    public $queue = 'zhijin_create';
    
    // Redis连接配置
    public $connection = 'default';
    
    // 消费逻辑
    public function consume($data)
    {
        try {    
            $start_time = microtime(true);
            $table_name = $data['table_name']; 
            $insert_data = $data['data']; 
            $is_exist_new = $result = Db::table($table_name)
            ->where('the_date', $insert_data['the_date'])
            ->where('gp_code', $insert_data['gp_code'])
            ->first(); 
            if($is_exist_new){ 
                $update_data  = [
                    'last_price' => $price,
                    'last_rise_fall' => $rise_fall,
                    'last_zljlr_je' => $zljlr_je,
                    'last_zljlr_jzb' => $zljlr_jzb,
                    'update_time' => date('Y-m-d H:i:s')
                ];
                $res = Db::table($table_name)
                ->where('id', $is_exist_new['id'])
                ->update($update_data);
            }else{
                $res = Db::table($table_name)->insert($insert_data);
            } 
            //echo date("Y-m-d H:i:s")."【zjlr_create】-处理{$insert_data['gp_code']}成功"."\r\n";  
            return true; 
        } catch (\Throwable $e) {
            Log::error("资金流向处理失败: {$e->getLine()}  {$e->getMessage()}");
            return false; // 触发重试
        }
    } 
    
    /**
     * 爬虫记录
     * @param unknown $code
     * @return mixed
     */
    public static function getBaseXueQiuCurl($code) {
        $url = "https://stock.xueqiu.com/v5/stock/history/trade.json?symbol=$code&count=10";
        $ch = curl_init ();
        $header [] = "Host: stock.xueqiu.com";
        $header [] = "Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4";
        $header [] = "Cookie: xq_a_token=abfd9c65d471bdcdac59a60cf0194d3590d3285d;xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjEwMzU3NDY4MDUsImlzcyI6InVjIiwiZXhwIjoxNzQ1NTYzMjE4LCJjdG0iOjE3NDI5NzE3MjkwMjQsImNpZCI6Ikp0WGJhTW43ZVAifQ.isvglkTj-HtlzlHvHqePMP-pSWqFapRlhain8T5XlxGKUH-BsUbt235NFLC9Qz10m9olW2W_mTEz-tLajleAxJO8FugKVqpUOZoUL08fiXwDxkbj1Fr-g7GNdiO6yFIeiFBvFTvVz2H5Tjj1O0vodvzKQF7U39Ai5qhhufRptafkMYUwcnfQcToWnKxka7O5UTVl_ysRoq5bPcyRuJ63uhb3DhconxIxVQ37KODwNz1fnWEKjIKiFK9gUqd-cbkRTOJpPGWZ2M1KfpeP3FE5RhyEQB8QGBjKviYMhFW-adQXzh2aS-DX3eQVObrrVUSw25ZwKmCNy-t1W1MykYqqzw;u=1035746805;session_id=;xid=0";
        $header [] = "X-Device-ID: 1XIAOMIdbdbe53579ad35347ace0e8848be9efe";
        $header [] = "X-Device-Model-Name: HUAWEI_TAS-AN00";
        $header [] = "X-Device-OS: Android 9";
        $header [] = "Accept-Encoding: gzip";
        $header [] = "Connection: Keep-Alive";
        
        $xforip = rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 );
        $header [] = "X-FORWARDED-FOR: $xforip";
        $header [] = "CLIENT-IP: $xforip";
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        curl_setopt ( $ch, CURLOPT_ENCODING, "" );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 3 ); // 设置1秒超时
        curl_setopt ( $ch, CURLOPT_USERAGENT, "Xueqiu Android 14.25" );
        $output = curl_exec ( $ch );
        // $output = mb_convert_encoding($output, 'utf-8', 'GBK,UTF-8,ASCII');
        curl_close ( $ch );
        return $output;
    } 
} 